#include "../include/Port_Global.h"
#include "../include/Portfolio.h"


void resumemodule(char *name, DATAFILE *image, FONT *Japanese_Fonts)
{
//Reset Mouse Wheel
	position_mouse_z(0);
	scare_mouse();
//Generate Back Buffer	
	BITMAP *B_Buffer = create_bitmap(800,600);
//Generate Text for Game
	char direc[64] = "PORT/RESUME/";
	strcat(direc,name);
	strcat(direc,"_G.dat");

//Load Graphic file for the game
	DATAFILE *GF = load_datafile(direc);
	
	int current_pic = 0;
	int mouseoldz = 0;
	int quit = 0;
	int lock = 0;
//NO TITLE just load images
	int state = 2;
	int collided = 0;
	int buttonroll = 0;
//Mouse checks
	int mouselock = 0;
	int mousecounter = 0;
//For the textscroller
	int scroll_position = 0;

//Mouse hit detection image
	PMASK *submenubutton_pmask = create_allegro_pmask((BITMAP *)image[Submenubuttonpmask].dat);
	BITMAP *B_mousehit = create_bitmap(1,1);
	PMASK *mousehit_pmask = create_allegro_pmask(B_mousehit);


//Start Main Loop
	while(!key[KEY_ESC] && quit == 0)//If the user hits escape, quit the program
	{
		while(Portfolio::ticks > 0)
		{
			if(mouselock == 1)
			{
				if(mouse_b == 0)
					mouselock = 0;
			}

			if(mouselock == 2)
			{
				mousecounter++;
				if(mousecounter > 10)
				{
					mousecounter = 0;
					mouselock= 0;
				}
			}

			collided = check_pmask_collision(submenubutton_pmask,mousehit_pmask,38,510,mouse_x,mouse_y);

			if( collided == 1)
			{
				if(mouse_x < 90)
					buttonroll = 1;
				if(mouse_x > 100 && mouse_x < 220)
					buttonroll = 2;
				if(mouse_x > 248)
					buttonroll = 3;
			}
			else
			{
				buttonroll = 0;
			}

//Exit back to previous menu
			if(buttonroll == 1 && mouse_b ==1 && mouselock == 0)
			{
				mouselock = 1;
				quit = 1;
			}
			if(buttonroll == 2 && mouse_b == 1 && mouselock == 0)
			{	
				mouselock = 1;
				state = 2;				
			}

			if(buttonroll == 3 && mouse_b == 1 && mouselock == 0)
			{
				mouselock = 1;
				state = 1;
			}

			Portfolio::ticks --;
		}//End while(Portfolio::ticks > 0)


//==========================================BEGIN ON SCREEN DRAWING==========================================================//
//===========================================================================================================================//
		acquire_screen();
//DRAW GUI BUTTONS
		draw_sprite(B_Buffer,(BITMAP *)image[Submenu].dat,0,0);
		//blit((BITMAP *)image[Submenu].dat,0,0,38,

		if(buttonroll == 1)
			blit((BITMAP *)image[Submenubutton].dat,B_Buffer,0,0,38,510,70,76);
		if(buttonroll == 2)
			blit((BITMAP *)image[Submenubutton].dat,B_Buffer,70,0,108,510,141,76);
		if(buttonroll == 3)
			blit((BITMAP *)image[Submenubutton].dat,B_Buffer,210,0,248,510,144,76);
		
		if(state == 2)
		{
			blit((BITMAP *)GF[0].dat,B_Buffer,0,scroll_position*50			,52,110,700,400);
			blit((BITMAP *)GF[1].dat,B_Buffer,0,((scroll_position)*50)-1106	,52,110,700,400);

			int linetrip;
			int moused = 0;
			int dx = BUTA_X - mouse_x;
			int dy = BUTA_Y - mouse_y;

			vline(B_Buffer, BUTA_X, BUTA_Y, BUTB_Y, makecol(255,255,255));
						
			//Draw the horizontal scroll bar.
			vline(B_Buffer, BUTA_X  , BUTA_Y, BUTB_Y, C_ScrollBar);
			vline(B_Buffer, BUTA_X+1, BUTA_Y, BUTB_Y, C_ScrollBar);
			vline(B_Buffer, BUTA_X-1, BUTA_Y, BUTB_Y, C_ScrollBar);

			linetrip = (BUTB_Y - BUTA_Y)/35;

			vline(B_Buffer,BUTA_X, BUTA_Y + linetrip * scroll_position, BUTA_Y + linetrip * scroll_position + linetrip,C_ScrollBarSmall);
//CALC DISTANCE
			moused = (dx*dx) + (dy*dy);
			triforce(B_Buffer,BUTA_X,BUTA_Y,C_ScrollBarButton);
//TEXT SCROLLER
			if(moused < 49)
			{
				triforce(B_Buffer,BUTA_X,BUTA_Y,C_ScrollBarButtonHit);
				if(mouse_b == 1 && mouselock == 0)
				{
					scroll_position--;
					if(scroll_position < 0)
						scroll_position = 0;
					mouselock = 2;
				}
			}

			dx = BUTB_X - mouse_x;
			dy = BUTB_Y - mouse_y;
//CALC DISTANCE
			moused = (dx*dx) + (dy*dy);			
			triforce(B_Buffer,BUTA_X,BUTB_Y,C_ScrollBarButton,-10);
			if(moused < 49)
			{
				triforce(B_Buffer,BUTA_X,BUTB_Y,C_ScrollBarButtonHit,-10);
				if(mouse_b == 1 && mouselock == 0)
				{
					scroll_position++;
					if(scroll_position > 35)		
						scroll_position = 35;
					mouselock = 2;
				}
			}
//WHEEL MOUSE
			if(mouse_z < mouseoldz || key[KEY_DOWN])
			{
				scroll_position++;
				if(scroll_position > 35)			
					scroll_position = 35;
				mouseoldz = mouse_z;
			}
			if(mouse_z > mouseoldz || key[KEY_UP])
			{
				scroll_position--;
				if(scroll_position < 0)
					scroll_position = 0;
				mouseoldz = mouse_z;
			}

			textprintf(B_Buffer, Japanese_Fonts,390,530, C_ScrollBarSmall,defaultmsg[9]);			
		}
		

		moduletexthelp(B_Buffer,Japanese_Fonts,buttonroll);
		rectfill(B_Buffer,mouse_x - 3,mouse_y - 3,mouse_x + 3, mouse_y + 3, makecol(0,0,0));	
		rectfill(B_Buffer,mouse_x - 2,mouse_y - 2,mouse_x + 2, mouse_y + 2, makecol(255,255,255));	
		blit(B_Buffer,screen,0,0,0,0,800,600);
		release_screen();
	}
	
	destroy_pmask(mousehit_pmask);
	release_bitmap(B_mousehit);
	destroy_pmask(submenubutton_pmask);
	unload_datafile(GF);
	release_bitmap(B_Buffer);
	clear_keybuf();

}
